﻿Imports System
Imports System.Collections.Generic
Imports System.Reflection
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities
    Friend NotInheritable Class WebServiceResponseValidator
        Inherits ActivityValidator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim activity As WebServiceOutputActivity = [TryCast](obj,WebServiceOutputActivity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(WebServiceOutputActivity).FullName }), "obj")
            End If
            If Not Helpers.IsActivityLocked(activity) Then
                Dim list As List([Of] ParameterInfo)
                Dim list2 As List([Of] ParameterInfo)
                Dim activity2 As WebServiceInputActivity = Nothing
                If String.IsNullOrEmpty(activity.InputActivityName) Then
                    errors.Add(ValidationError.GetNotSetValidationError("InputActivityName"))
                    Return errors
                End If
                Dim service As ITypeProvider = DirectCast(manager.GetService(GetType(ITypeProvider)), ITypeProvider)
                If (service Is Nothing) Then
                    Throw New InvalidOperationException(SR.GetString("General_MissingService", New Object() { GetType(ITypeProvider).FullName }))
                End If
                Dim flag As Boolean = False
                Dim activity3 As Activity
                For Each activity3 In WebServiceActivityHelpers.GetPreceedingActivities(activity)
                    If ((Not TypeOf activity3 Is WebServiceOutputActivity OrElse (String.Compare(DirectCast(activity3, WebServiceOutputActivity).InputActivityName, activity.InputActivityName, StringComparison.Ordinal) <> 0)) AndAlso (Not TypeOf activity3 Is WebServiceFaultActivity OrElse (String.Compare(DirectCast(activity3, WebServiceFaultActivity).InputActivityName, activity.InputActivityName, StringComparison.Ordinal) <> 0))) Then
                        [Continue] For
                    End If
                    If TypeOf activity3 Is WebServiceOutputActivity Then
                        errors.Add(New ValidationError(SR.GetString("Error_DuplicateWebServiceResponseFound", New Object() { activity3.QualifiedName, activity.InputActivityName }), &H56A))
                        Return errors
                    End If
                    errors.Add(New ValidationError(SR.GetString("Error_DuplicateWebServiceFaultFound", New Object() { activity3.QualifiedName, activity.InputActivityName }), &H574))
                    Return errors
                Next
                Dim activity4 As Activity
                For Each activity4 In WebServiceActivityHelpers.GetPreceedingActivities(activity)
                    If (String.Compare(activity4.QualifiedName, activity.InputActivityName, StringComparison.Ordinal) = 0) Then
                        If TypeOf activity4 Is WebServiceInputActivity Then
                            activity2 = [TryCast](activity4,WebServiceInputActivity)
                            flag = True
                            Exit For
                        End If
                        flag = False
                        errors.Add(New ValidationError(SR.GetString("Error_WebServiceReceiveNotValid", New Object() { activity.InputActivityName }), &H564))
                        Return errors
                    End If
                Next
                If Not flag Then
                    errors.Add(New ValidationError(SR.GetString("Error_WebServiceReceiveNotFound", New Object() { activity.InputActivityName }), &H55E))
                    Return errors
                End If
                Dim interfaceType As Type = Nothing
                If (Not activity2.InterfaceType Is Nothing) Then
                    interfaceType = service.GetType(activity2.InterfaceType.AssemblyQualifiedName)
                End If
                If (interfaceType Is Nothing) Then
                    errors.Add(New ValidationError(SR.GetString("Error_WebServiceReceiveNotConfigured", New Object() { activity2.Name }), &H566))
                    Return errors
                End If
                If String.IsNullOrEmpty(activity2.MethodName) Then
                    errors.Add(New ValidationError(SR.GetString("Error_WebServiceReceiveNotConfigured", New Object() { activity2.Name }), &H566))
                    Return errors
                End If
                Dim interfaceMethod As MethodInfo = Helpers.GetInterfaceMethod(interfaceType, activity2.MethodName)
                If (interfaceMethod Is Nothing) Then
                    errors.Add(New ValidationError(SR.GetString("Error_WebServiceReceiveNotConfigured", New Object() { activity2.Name }), &H566))
                    Return errors
                End If
                Dim errors2 As ValidationErrorCollection = WebServiceActivityHelpers.ValidateParameterTypes(interfaceMethod)
                If (errors2.Count > 0) Then
                    Dim error As ValidationError
                    For Each error In errors2
                        [error].PropertyName = "InputActivityName"
                    Next
                    errors.AddRange(errors2)
                    Return errors
                End If
                WebServiceActivityHelpers.GetParameterInfo(interfaceMethod, list, list2)
                If (list2.Count = 0) Then
                    errors.Add(New ValidationError(SR.GetString("Error_WebServiceResponseNotNeeded"), &H565))
                    Return errors
                End If
                Dim info2 As ParameterInfo
                For Each info2 In list2
                    Dim name As String = info2.Name
                    Dim type2 As Type = If(info2.ParameterType.IsByRef, info2.ParameterType.GetElementType, info2.ParameterType)
                    If (info2.Position = -1) Then
                        name = "(ReturnValue)"
                    End If
                    Dim binding As Object = Nothing
                    If activity.ParameterBindings.Contains(name) Then
                        If activity.ParameterBindings.Item(name).IsBindingSet(WorkflowParameterBinding.ValueProperty) Then
                            binding = activity.ParameterBindings.Item(name).GetBinding(WorkflowParameterBinding.ValueProperty)
                        Else
                            binding = activity.ParameterBindings.Item(name).GetValue(WorkflowParameterBinding.ValueProperty)
                        End If
                    End If
                    If (Not type2.IsPublic OrElse Not type2.IsSerializable) Then
                        Dim item As New ValidationError(SR.GetString("Error_TypeNotPublicSerializable", New Object() { name, type2.FullName }), &H567) { _
                            .PropertyName = If((String.Compare(name, "(ReturnValue)", StringComparison.Ordinal) = 0), name, ParameterInfoBasedPropertyDescriptor.GetParameterPropertyName(activity2.GetType, name)) _
                        }
                        errors.Add(item)
                    Else
                        If (Not activity.ParameterBindings.Contains(name) OrElse (binding Is Nothing)) Then
                            Dim notSetValidationError As ValidationError = ValidationError.GetNotSetValidationError(name)
                            notSetValidationError.PropertyName = If((String.Compare(name, "(ReturnValue)", StringComparison.Ordinal) = 0), name, ParameterInfoBasedPropertyDescriptor.GetParameterPropertyName(activity2.GetType, name))
                            errors.Add(notSetValidationError)
                            [Continue] For
                        End If
                        Dim read As AccessTypes = AccessTypes.Read
                        If ((info2.IsOut OrElse info2.IsRetval) OrElse (info2.Position = -1)) Then
                            read = AccessTypes.Write
                        End If
                        Dim errors3 As ValidationErrorCollection = ValidationHelpers.ValidateProperty(manager, activity, binding, New PropertyValidationContext(activity.ParameterBindings.Item(name), Nothing, name), New BindValidationContext(If(info2.ParameterType.IsByRef, info2.ParameterType.GetElementType, info2.ParameterType), read))
                        Dim error4 As ValidationError
                        For Each error4 In errors3
                            If (String.Compare(name, "(ReturnValue)", StringComparison.Ordinal) <> 0) Then
                                error4.PropertyName = ParameterInfoBasedPropertyDescriptor.GetParameterPropertyName(activity2.GetType, name)
                            End If
                        Next
                        errors.AddRange(errors3)
                    End If
                Next
                If (activity.ParameterBindings.Count > list2.Count) Then
                    errors.Add(New ValidationError(SR.GetString("Warning_AdditionalBindingsFound"), &H630, True))
                End If
            End If
            Return errors
        End Function

    End Class
End Namespace

